﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.model.Instance;
using l1cs.server.utils;
using System.Collections;

namespace l1cs.server.datatables
{
    public class TownTable
    {
        private static Logger _log = Logger.getLogger( typeof( TownTable ).FullName );

        private static TownTable _instance;

        private Hashtable _towns = Hashtable.Synchronized( new Hashtable() );

        public static TownTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new TownTable();
            }
            return _instance;
        }

        private TownTable()
        {
            load();
        }

        public void load()
        {/*TTOK*/
            _towns.Clear();
            int townid;
            String sqlstr = "SELECT * FROM town";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1Town town = new L1Town();
                    townid = SqlUtils.toInt( item["town_id"] );
                    town.set_townid( townid );
                    town.set_name( Convert.ToString( item["name"] ) );
                    town.set_leader_id( SqlUtils.toInt( item["leader_id"] ) );
                    town.set_leader_name( Convert.ToString( item["leader_name"] ) );
                    town.set_tax_rate( SqlUtils.toInt( item["tax_rate"] ) );
                    town.set_tax_rate_reserved( SqlUtils.toInt( item["tax_rate_reserved"] ) );
                    town.set_sales_money( SqlUtils.toInt( item["sales_money"] ) );
                    town.set_sales_money_yesterday( SqlUtils.toInt( item["sales_money_yesterday"] ) );
                    town.set_town_tax( SqlUtils.toInt( item["town_tax"] ) );
                    town.set_town_fix_tax( SqlUtils.toInt( item["town_fix_tax"] ) );

                    _towns[townid] = town;
                }
            }
        }

        public L1Town[] getTownTableList()
        {/*TTOK*/
            // TODO 優先度5 Hashtableのコピー渡しよりいい方法はないか？
            L1Town[] ret = new L1Town[_towns.Count];
            _towns.Values.CopyTo( ret, 0 );
            return ret;
        }

        public L1Town getTownTable( int id )
        {/*TTOK*/
            L1Town ret = (L1Town)_towns[id];
            return ret;
        }

        public bool isLeader( L1PcInstance pc, int town_id )
        {/*TTOK*/
            L1Town town = getTownTable( town_id );
            return ( town.get_leader_id() == pc.getId() );
        }

        public void addSalesMoney( int town_id, int salesMoney )
        {/*TTOK*/
            lock ( this )
            {
                L1Town town = TownTable.getInstance().getTownTable( town_id );
                int townTaxRate = town.get_tax_rate();

                int townTax = salesMoney / 100 * townTaxRate;
                int townFixTax = salesMoney / 100 * 2;

                if ( townTax <= 0 && townTaxRate > 0 )
                {
                    townTax = 1;
                }
                if ( townFixTax <= 0 && townTaxRate > 0 )
                {
                    townFixTax = 1;
                }
                String sqlstr = @"UPDATE town SET 
                                    sales_money   = sales_money  + ?1, 
                                    town_tax      = town_tax     + ?2, 
                                    town_fix_tax  = town_fix_tax + ?3 
                                    WHERE town_id = ?4";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32).Value = salesMoney;
                    cmd.Parameters.Add( "?2", MySqlDbType.Int32).Value = townTax;
                    cmd.Parameters.Add( "?3", MySqlDbType.Int32).Value = townFixTax;
                    cmd.Parameters.Add( "?4", MySqlDbType.Int32).Value = town_id;
                    wrapper.ExecNonQuery( cmd );
                }
                town.set_sales_money( town.get_sales_money() + salesMoney );
                town.set_town_tax( town.get_town_tax() + townTax );
                town.set_town_fix_tax( town.get_town_fix_tax() + townFixTax );
            }
        }

        public void updateTaxRate()
        {/*TTOK*/
            String sqlstr = "UPDATE town SET tax_rate = tax_rate_reserved";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                wrapper.ExecNonQuery( cmd );
            }

        }

        public void updateSalesMoneyYesterday()
        {/*TTOK*/
            String sqlstr = "UPDATE town SET sales_money_yesterday = sales_money, sales_money = 0";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                wrapper.ExecNonQuery( cmd );
            }
        }
    }
}
